---@class BuffEffectManager
BuffEffectManager = ClientFight.CreateClass("BuffEffectManager")
local this = BuffEffectManager
---@type table<number,IBuffEffect>
this.buffEffects = {}

---@param buffEffect IBuffEffect
function this.putBuffEffect(buffId,buffEffect)
    this.buffEffects[buffId] = buffEffect
end
---@return IBuffEffect
function this.getBuffEffect(id)
    local buffEffect = this.buffEffects[id]
    if buffEffect ~= nil then
        return buffEffect
    end
    return ClientFight.BaseBuffEffect;
end
---增加buff效果
---@param fight Fight
---@param buff FighterBuff
function this.addBuffEffect(fight,buff)
    local effect = this.getBuffEffect(buff.buffBean.f_BuffEffect)
    if effect then
        return effect:add(fight,buff)
    else
        return IBuffEffect.FAIL
    end
end
---移除buff效果
---@param fight Fight
---@param buff FighterBuff
function this.removeBuffEffect(fight,buff,force,...)
    local parameters = {...}
    if not force then
        local buffRemoveParameter = buff.buffBean.f_BuffRemoveParameter
        if buff.buffBean.f_BuffRemoveTrigger == FightEventEnum.AFTER_ATTACK or buff.buffBean.f_BuffRemoveTrigger == FightEventEnum.BEFORE_ATTACK then
            if #parameters >= 2 and parameters[2].__cname == FightUsingSkill.__cname then
                ---@type FightUsingSkill
                local trigger = parameters[2]
                if buffRemoveParameter ~= nil then
                    --移除参数
                    local triggerSkillId = buffRemoveParameter[1]
                    if triggerSkillId ~= 0 then
                        if triggerSkillId ~= trigger.skill.skillBean.f_SkillID then
                            return IBuffEffect.FAIL
                        end
                    end
                else
                    if buff.addParameters == nil or table.getn(buff.addParameters) < 2 then
                        return IBuffEffect.FAIL
                    end
                    if buff.addParameters[2] ~= trigger then
                        return IBuffEffect.FAIL
                    end
                end
            else
                SkillRoleMgr.savePathFile(string.format("[%s][Buff]%s的%s配置了攻击前或攻击后移除,但是没有找到技能,移除失败,请检查代码",
                         fight.frame, buff.source.fighterId, buff.modelId
                 ));
            end
            -- buff添加或移除时候触发移除，不填写参数时只有此BUFF添加才会触发，填写[0]时候任意buff都会触发，其他只有固定ID的BUFF才会触发
        elseif buff.buffBean.f_BuffRemoveTrigger == FightEventEnum.ADD_BUFF or buff.buffBean.f_BuffRemoveTrigger == FightEventEnum.REMOVE_BUFF then
            if table.getn(parameters) >= 1 and parameters[1].__cname == FighterBuff.__cname then
                ---@type FighterBuff
                local trigger = parameters[1]
                if buffRemoveParameter == nil then
                    if(buff.id ~= trigger.id) then
                        return IBuffEffect.FAIL
                    end
                elseif buffRemoveParameter[1] == 0 then--=0的情况下,所有buff都行
                else
                    local contains = false
                    for k,buffId in pairs(buffRemoveParameter) do --包含任意一个,都算成功
                        if buffId == trigger.modelId then
                            contains = true
                            break
                        end
                    end
                    if not contains then
                        return IBuffEffect.FAIL
                    end
                end
            else
                SkillRoleMgr.savePathFile(string.format("[%s][Buff]%s的%s配置了buff添加或移除时移除,但是没有找到buff,移除失败,请检查代码",
                         fight.frame, buff.source.fighterId, buff.modelId
                 ));
            end
        elseif buff.buffBean.f_BuffRemoveTrigger == FightEventEnum.REMOVE_BUFF_TARGET then
            if #parameters >= 1 and parameters[1].__cname == FighterBuff.__cname then
                ---@type FighterBuff
                local trigger = parameters[1]
                if buffRemoveParameter== nil then
                    return IBuffEffect.FAIL
                elseif buffRemoveParameter[1] ~= 0 and buffRemoveParameter[1] ~= trigger.modelId then
                    return IBuffEffect.FAIL
                else
                    --这个buff确实是我给添加的
                    if trigger.source ~= buff.source then
                        return IBuffEffect.FAIL
                    end
                end
            else
                return IBuffEffect.FAIL
            end
        end
        if not BuffConditionType:action(buff,buff.buffBean.buffRemoveCondition,...) then
            return IBuffEffect.FAIL
        end

    end
    local effect = this.getBuffEffect(buff.buffBean.f_BuffEffect)
    buff.removeEffect = true
    return effect:remove(fight,buff,force,...)
end
---行为触发BUFF (比如攻击,被攻击,跳跃,死亡 等等等等)
---@param fight Fight
---@param buff FighterBuff
function this.actionBuffEffect(fight,buff,...)
    local fail = IBuffEffect.FAIL
    local parameters = {...}
    --// 非永久BUFF
    --// 移除时机 == 0的时候触发后移除
    if buff.buffBean.f_BuffRemoveTrigger == FightEventEnum.TRIGGER_BUFF then
        fail = IBuffEffect.REMOVE
    end
    buff.inTriggerTimes = buff.inTriggerTimes + 1
    if not BuffConditionType:action(buff,buff.buffBean.f_BuffCondition,...) then
        return fail
    end
    --buff添加或移除时候触发，不填写参数时只有此BUFF添加才会触发，填写[0]时候任意buff都会触发，其他只有固定ID的BUFF才会触发
    if buff.buffBean.f_BuffActionTrigger == FightEventEnum.ADD_BUFF or buff.buffBean.f_BuffActionTrigger == FightEventEnum.REMOVE_BUFF then
        if(parameters[1].__cname == FighterBuff.__cname) then
            ---@type FighterBuff
            local trigger = parameters[1]
            if buff.buffBean.f_BuffTrigger == nil then
                if buff.id ~= trigger.id then
                    return IBuffEffect.FAIL
                end
            elseif buff.buffBean.f_BuffTrigger[1] ~= 0 and buff.buffBean.f_BuffTrigger[1] ~= trigger.modelId then
                return IBuffEffect.FAIL
            end
        end
    elseif buff.buffBean.f_BuffActionTrigger == FightEventEnum.REMOVE_BUFF_TARGET then
        if parameters[1].__cname == FighterBuff.__cname then
            ---@type FighterBuff
            local trigger =  parameters[1]
            if (buff.buffBean.f_BuffTrigger == nil) then
                return fail;
            elseif (buff.buffBean.f_BuffTrigger[1] ~= 0 and buff.buffBean.f_BuffTrigger[1] ~= trigger.modelId) then
                return fail;
            else
                if (trigger.source ~= buff.target) then
                    return fail;
                end
            end
        else
            return fail
        end
    end
    local effect = this.getBuffEffect(buff.buffBean.f_BuffEffect)
    local result = effect:action(fight,buff,...)
    if(bit.band(result,IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0  or bit.band(result,IBuffEffect.SUCCESS_ATTRI_NO_CHANGE) > 0) then
        buff.triggerTimes = buff.triggerTimes + 1
        buff.triggerFrame = fight.frame
    end
    if buff.buffBean.f_BuffRemoveTrigger == FightEventEnum.TRIGGER_BUFF then
        --触发之后就移除的
        return IBuffEffect.REMOVE
    end
    if buff.buffBean.f_BuffRemoveTrigger == FightEventEnum.TRIGGER_BUFF_SUCCESS then
        local buffRemoveParameter = buff.buffBean.f_BuffRemoveParameter
        if(buffRemoveParameter ~= nil and table.getn(buffRemoveParameter) > 0) then--配置了移除条件
            local removeTimes = buffRemoveParameter[1]
            if buff.triggerTimes >= removeTimes then
                return IBuffEffect.REMOVE
            else
                return result
            end
        end
        --触发成功需要移除的
        if bit.band(result,IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0 or bit.band(result,IBuffEffect.SUCCESS_ATTRI_NO_CHANGE) > 0 then
            return IBuffEffect.REMOVE
        end
    end
    return result
end